<?php 
/**
 * 接口Controller基类
 * @package Core
 * @author 江羿平<jyp@cjkt.com>
 */
namespace Core;
use Libraries\Token;
use OAuth2\Storage\CIDatabase;
use OAuth2\Storage\Redis;
use Predis\Client;
use Models\User;

class API_Controller extends Controller
{
	/**
	 * @var 客户端，用户，权限存储器
	 */
	protected $storage = null;

	/**
	 * @var access_token, refresh_token存储器
	 */
	protected $tokenStorage = null;

	/**
	 * 表单验证错误
	 * 
	 * @param string 错误字段标签
	 * @param string 错误信息
	 * @param array 所有错误信息
	 */
	public function validateError($error, $error_msg = '')
	{
		$this->output->set_status_header(422)
			->set_content_type('application/json');
		if (is_array($error)) {
			foreach ($error as $key => $value) {
				$this->output->set_output(json_encode(array(
					'error' => $key,
					'error_msg' => $value,
					'errors' => $error
				)));
			}
		} else {
			$this->output->set_output(json_encode(array(
				'error' => $error,
				'error_msg' => $error_msg,
				'errors' => array(
						$error => $error_msg
					)
			)));
		}
		
		$this->output->_display();
		die;
	}

	/**
	 * 执行成功
	 *
	 * @param array
	 * @param string
	 */
	public function success($data = array(), $msg = '')
	{
		if(empty($data)) {
			$data['error_msg'] = 'success';
		}

		$this->output->set_status_header(200)
			->set_content_type('application/json')
			->set_output(json_encode($data));
	}

	/**
	 * 执行失败
	 *
	 * @param string
	 */
	public function error($error_msg='error')
	{
		$this->output->set_status_header(500)
			->set_content_type('application/json')
			->set_output(json_encode(array('error' => $error_msg)));
	}

	/**
	 * 响应
	 * 生成一个响应
	 * @param mixed 数据
	 * @param int 状态码
	 * @param array 请求头
	 */
	public function response($data = null, $status = 200, array $headers = array(), $json = false)
	{
		if (is_array($data) OR $json) {
			$this->output->set_content_type('application/json');
			$this->output->set_output(json_encode($data));
		} else {
			$this->output->set_output($data);
		}

		$this->output->set_status_header($status);
		
		foreach ($headers as $key => $value) {
			if (is_integer($key)) {
				$this->output->set_header($value);
			} else {
				$this->output->set_header($key.': '.$value);
			}
		}
	}

	/**
	 * 创建禁止响应
	 */
	public function forbiddenResponse($msg = '您无权编辑')
	{
		$this->output->set_status_header(403)
			->set_content_type('application/json')
			->set_output(json_encode(array('error' => 'forbidden', 'error_msg' => $msg)));
	}

	public function notFoundResponse($msg = '未找到该资源')
	{
		$this->output->set_status_header(404)
			->set_content_type('application/json')
			->set_output(json_encode(array('error' => 'not found','error_msg' => $msg)));
	}

	/**
	 * 获取持久存储器
	 *
	 * @param array config
	 * @return OAuth2\Storage\CIDatabase
	 */
	public function getStorage($config = array())
	{
		if ($this->storage === null) {
			$this->storage = new CIDatabase($this->db, $config);
		}

		return $this->storage;
	}

	/**
	 * 获取token存储器
	 *
	 * @param array 配置
	 * - scheme: 
	 * - host:
	 * - port:
	 * - password:
	 * @return OAuth2\Storage\Redis
	 */
	public function getTokenStorage($userConfig = array())
	{
		if ($this->tokenStorage === null) {
			$config = array_merge(array(
					'scheme' => 'tcp',
		            'host' => REDIS_HOST,
		            'port' => REDIS_PORT,
		            'password' => 'local'
				), $userConfig);
			$this->tokenStorage = new Redis(new Client($config));
		}

		return $this->tokenStorage;
	}
}
